package com.keya.pacs;

import org.apache.commons.lang.StringUtils;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * .
 *
 * @author : xf.he
 * @version : 1.0 2023/11/15
 * @since : 1.0
 **/
public class StudyDescriptionFilter {

    List<String> blacklist;
    List<String> whitelist;

    public StudyDescriptionFilter(String[] blacklist, String[] whitelist) {
        this.blacklist = normalizeKeywords(blacklist);
        this.whitelist = normalizeKeywords(whitelist);
    }

    private static List<String> normalizeKeywords(String[] array) {
        if (array == null) {
            return List.of();
        }
        return Stream.of(array)
                .map(StringUtils::trim)
                .filter(StringUtils::isNotEmpty)
                .collect(Collectors.toUnmodifiableList());
    }

    private static boolean matchKeyword(String description, String keyword) {
        return StringUtils.equals("*", keyword) || StringUtils.containsIgnoreCase(description, keyword);
    }

    public boolean shouldExclude(String description) {
        boolean reject = blacklist.stream().anyMatch(keyword -> matchKeyword(description, keyword));
        if (reject) {
            return true;
        }
        if (whitelist.isEmpty()) {
            return false;
        }
        return whitelist.stream().noneMatch(keyword -> matchKeyword(description, keyword));
    }
}
